# -*- coding: utf-8 -*-

from django.db import models
from core.models import CoreModel, table_prefix
from django.utils.translation import gettext_lazy as _
import reversion


@reversion.register()
class TestPlan(CoreModel):
    product = models.ForeignKey(
        to="pms.Product",
        related_name="testplan_set",
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="所属产品",
    )
    owner = models.ForeignKey(
        to="sys.User",
        related_name="testplan_owner_set",
        on_delete=models.DO_NOTHING,
        db_constraint=False,
        verbose_name="负责人",
    )
    requirements = models.ManyToManyField(
        blank=True,
        to="pms.Requirement",
        related_name="testplan_set",
        db_constraint=False,
        verbose_name="关联需求",
    )
    testcases = models.ManyToManyField(
        blank=True,
        to="tms.TestCase",
        related_name="testplan_set",
        db_constraint=False,
        verbose_name="关联用例",
    )
    version = models.ForeignKey(
        to="pms.Version",
        related_name="testplan_set",
        on_delete=models.CASCADE,
        db_constraint=False,
        verbose_name="所属版本",
    )
    name = models.CharField(max_length=256, verbose_name="计划名称")
    scheduled_start = models.DateTimeField(
        null=True, blank=True, verbose_name="计划启动时间"
    )
    scheduled_end = models.DateTimeField(
        null=True, blank=True, verbose_name="计划完成时间"
    )
    actual_start = models.DateTimeField(
        null=True, blank=True, verbose_name="实际启动时间"
    )
    actual_end = models.DateTimeField(
        null=True, blank=True, verbose_name="实际完成时间"
    )

    def __str__(self) -> str:
        return self.name

    class Meta:
        db_table = table_prefix + "tms_testplan"
        verbose_name = "测试计划"
        verbose_name_plural = verbose_name
        ordering = ("-update_time",)
